﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.Drawing;
using DLLTest.OpertionClass;
using DHNetSDK;

namespace DLLTest.OpertionClass
{
    /// <summary>
    /// 用于视频界面的分屏
    /// </summary>
    class PanelOpertion
    {
        #region 属性
        /// <summary>
        /// 记录所选 Panel
        /// </summary>
        public static Panel SelectedPanel { get; private set; }

        /// <summary>
        /// 记录所有系统新创建的 Panel 数组
        /// </summary>
        public static List<Panel> PanelCurrentList
        {
            get
            {
                return panelCurrentList;
            }
        }
        #endregion

        #region 变量
        private static List<Panel> panelCurrentList = new List<Panel>(); // 记录所有系统新创建的 Panel 数组
        private static List<Panel> panelHistoryList = new List<Panel>(); // 记录上一次系统创建的 Panel 数组

        //画面分割
        SplitScreen splitscreen = new SplitScreen();
        int split = 0;
        #endregion

        /// <summary>
        /// 分屏
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="plVideo1"></param>
        public void SplitScreen(int channel, Panel plVideo1)
        {
            //测试分割画面,单画面
            plVideo1.Controls.Clear();//先清除 Panle 存在的内容。
            //plVideo1.Refresh();
            IList<Rectangle> rectList = splitscreen.CalcPanelRectangle(channel, plVideo1.Size);
            split = channel;

            // 1. 将历史正在播放的视频停止
            foreach (Panel currPanel in panelCurrentList)
            {
                if (currPanel.Tag != null &&
                    currPanel.Tag.ToString().Split(',').Length == 7)
                {
                    string[] leafTag = currPanel.Tag.ToString().Split(',');
                    DHClient.DHStopRealPlay(Convert.ToInt32(leafTag[6])); // 停止实时监控，需要传递实时监控句柄
                }
            }

            int historyPanelIndex = 0; // 历史 Panel 序号
            panelHistoryList.Clear(); // 清空历史 Panel 列表
            foreach (Panel currentPanel in panelCurrentList) // 2. 将新创建的数组放入历史数组中
            {
                panelHistoryList.Add(currentPanel);
            }
            panelCurrentList.Clear(); // 清空当前 Panel 列表


            foreach (Rectangle rect in rectList)
            {
                Panel panel = new Panel()
                {
                    BackColor = Color.Black,
                    Width = rect.Width,
                    Height = rect.Height,
                    Left = rect.X,
                    Top = rect.Y,
                };

                // 3. 播放视频
                if (panelHistoryList.Count > 0 && historyPanelIndex < panelHistoryList.Count &&
                    panelHistoryList[historyPanelIndex].Tag != null &&
                    panelHistoryList[historyPanelIndex].Tag.ToString().Split(',').Length == 7) // PanelTag: ip,port,user,pwd,loginID,channel,realPlayHandle
                {
                    panel.Tag = panelHistoryList[historyPanelIndex].Tag.ToString().Substring(0,
                        panelHistoryList[historyPanelIndex].Tag.ToString().LastIndexOf(',')); // 去 realPlayHandle

                    string[] panelTag = panel.Tag.ToString().Split(',');
                    int pRealPlayHandle = DHClient.DHRealPlay(int.Parse(panelTag[4]),
                        Convert.ToInt32(panelTag[5]), panel.Handle); //只取数据不播放

                    panel.Tag = string.Format("{0},{1}", panel.Tag, pRealPlayHandle); // 得到最新的 PanelTag: ip,port,user,pwd,loginID,channel,realPlayHandle
                }

                historyPanelIndex++;
                panel.Click += (s, e) =>
                {
                    //需要一个判断，改变或者恢复其颜色
                    SelectedPanel = s as Panel;
                    PaintPanelBorder(SelectedPanel, Color.LightGreen, 2, ButtonBorderStyle.Solid);
                };

                panel.DoubleClick += (s, e) =>
                {
                    if (panel.Tag != null &&
                        panel.Tag.ToString().Split(',').Length == 7)
                    {
                        frm_Main.BasePanel.Visible = false;
                        frm_Main.MaxPanel.Visible = true;

                        // a. 先停止小窗口中的视频
                        string[] leafTag = panel.Tag.ToString().Split(',');
                        DHClient.DHStopRealPlay(Convert.ToInt32(leafTag[6])); // 停止实时监控，需要传递实时监控句柄

                        // b. 再播放大窗口中的视频
                        frm_Main.MaxPanel.Tag = panel.Tag.ToString().Substring(0,
                            panel.Tag.ToString().LastIndexOf(',')); // 去 realPlayHandle

                        string[] maxPanelTag = frm_Main.MaxPanel.Tag.ToString().Split(',');
                        int pRealPlayHandle = DHClient.DHRealPlay(int.Parse(maxPanelTag[4]),
                            Convert.ToInt32(maxPanelTag[5]), frm_Main.MaxPanel.Handle); //只取数据不播放

                        frm_Main.MaxPanel.Tag = string.Format("{0},{1}", frm_Main.MaxPanel.Tag, pRealPlayHandle); // 得到最新的 MaxPanelTag: ip,port,user,pwd,loginID,channel,realPlayHandle
                    }
                };
                panelCurrentList.Add(panel); // 4. 加入到当前 Panel 列表
                plVideo1.Controls.Add(panel);
            }
        }


        /// <summary>
        /// 绘制面板边框,http://blog.163.com/y_p_xu/blog/static/1708571022011101265538446/
        /// </summary>
        /// <param name="pnl">要绘制边框的Panel</param>
        /// <param name="col">边框颜色</param>
        /// <param name="bWidth">边框宽度</param>
        /// <param name="bStyle">边框样式</param>
        private void PaintPanelBorder(Panel pnl, Color col, int bWidth, ButtonBorderStyle bStyle)
        {
            PaintEventArgs e = new PaintEventArgs(pnl.CreateGraphics(), pnl.ClientRectangle);

            ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle,
                col, bWidth, bStyle,
                col, bWidth, bStyle,
                col, bWidth, bStyle,
                col, bWidth, bStyle);
        }
    }
}
